import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI{

    IPSMP ipsmp;

    JFrame frame;
    JPanel panel;
    JPanel controlsPanel;
    JPanel radioButtonPanel;
    JPanel buttonPanel;
    JMenuBar menuBar;

    public Point location;

    JButton play;
    JButton pause;
    JButton next;
    JButton prev;
    JButton importer;

    JRadioButton shuffleMode;
    JRadioButton contMode;

    ButtonGroup modeButtons;

    PlayListener playListener ;
    PauseListener pauseListener;
    NextListener nextListener;
    PrevListener prevListener;
    ImportListener importListener;

    PlayModeListener playModeListener;

    JList<MP3File> library;
    JScrollPane scrollpane;

    public GUI(IPSMP i) {
	ipsmp = i;

	location = null;

	playListener = new PlayListener(ipsmp);
	pauseListener = new PauseListener(ipsmp);
	nextListener = new NextListener(ipsmp);
	prevListener = new PrevListener(ipsmp);
	importListener = new ImportListener(ipsmp);
	playModeListener = new PlayModeListener(ipsmp);

	paintGUI(ipsmp.playlistList.get(0));
    }

    public void paintGUI(MP3Playlist playlist) {
	try {
	    location = frame.getLocation();
	    frame.setVisible(false);
	}
	catch(NullPointerException E) {
	    System.out.println("NullPointerExcpetion caught: GUI has not yet been initialized. New GUI has been drawn (GUI: line 46)");
	}

	frame = new JFrame("IPSMP");
	panel = new JPanel(new BorderLayout());
	controlsPanel = new JPanel(new BorderLayout());
	radioButtonPanel = new JPanel(new FlowLayout());
	buttonPanel = new JPanel(new FlowLayout());



	//draw the menu bar
	menuBar = new JMenuBar();
	frame.setJMenuBar(menuBar);
	JMenu fileMenu = new JMenu("File");
	JMenu editMenu = new JMenu("Edit");
	JMenu viewMenu = new JMenu("View");
	JMenu helpMenu = new JMenu("Help");
	JMenu libraryMenu = new JMenu("Library");
	JMenu openMenu = new JMenu("Open");

	menuBar.add(fileMenu);
	menuBar.add(editMenu);
	menuBar.add(viewMenu);
	menuBar.add(helpMenu);

	JMenuItem importAction =   new JMenuItem("Import");
	JMenuItem openSongAction = new JMenuItem("Open Track");
	JMenuItem openPlaylistAction = new JMenuItem("Open Playlist");

	libraryMenu.add(importAction);

	openMenu.add(openSongAction);
	openMenu.add(openPlaylistAction);

	importAction.addActionListener(importListener);

	fileMenu.add(libraryMenu);
	fileMenu.add(openMenu);


	//get the list of imported songs in the library
	// Object[] objArray = ipsmp.masterplaylist.toArray();
	// MP3File[] mp3Array = new MP3File[objArray.length];

	// for (int j = 0; j < objArray.length; j++){
	//     mp3Array[j] = (MP3File)objArray[j];
	// } 
	library = new JList<MP3File>(ipsmp.currentPlaylist.toArray());
	scrollpane = new JScrollPane(library);

	library.setVisibleRowCount(7);
	scrollpane = new JScrollPane(library);
	panel.add(scrollpane, BorderLayout.NORTH);

	play = new JButton();
	play.addActionListener(playListener);
	play.setText("Play");
	buttonPanel.add(play);

	pause = new JButton();
	pause.addActionListener(pauseListener);
	pause.setText("Pause");
	buttonPanel.add(pause);

	prev = new JButton();
	prev.addActionListener(prevListener);
	prev.setText("Previous");
	buttonPanel.add(prev);

	next = new JButton();
	next.addActionListener(nextListener);
	next.setText("Next");
	buttonPanel.add(next);

	importer = new JButton();
	importer.addActionListener(importListener);
	importer.setText("Import");
	buttonPanel.add(importer);

	shuffleMode = new JRadioButton("Shuffle");
	shuffleMode.setActionCommand("shuffle");
	shuffleMode.addActionListener(playModeListener);

	contMode = new JRadioButton("Continuous");
	contMode.setActionCommand("continuous");
	contMode.addActionListener(playModeListener);
	contMode.setSelected(true);

	modeButtons = new ButtonGroup();
	modeButtons.add(contMode);
	modeButtons.add(shuffleMode);
	radioButtonPanel.add(contMode);
	radioButtonPanel.add(shuffleMode);

	controlsPanel.add(buttonPanel, BorderLayout.NORTH);
	controlsPanel.add(radioButtonPanel, BorderLayout.CENTER);

	panel.add(controlsPanel, BorderLayout.CENTER);

	frame.setSize(650,400);
	frame.getContentPane().add(panel);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	if (location != null)
	    frame.setLocation(location);
	frame.setVisible(true);
    }

    public class ImportListener implements ActionListener {
	IPSMP ipsmp;

	public ImportListener(IPSMP i){
	    ipsmp = i;
	}

	public void actionPerformed(ActionEvent a){
	   	  
	    JFileChooser chooser = new JFileChooser();
	    chooser.setDialogTitle("Import");
	    chooser.showOpenDialog(frame);
	    File temp = chooser.getSelectedFile();
	    if (temp.getPath().toLowerCase().endsWith(".mp3")) 
		ipsmp.importer(temp);
	    else {
		System.out.println("UserIsAKnuckleHeadError: User failed to select a valid file. (GUI: line 59)");
		JOptionPane.showMessageDialog(frame,"The selected file is not an mp3 file. This\nversion of IPSMP only supports mp3 files.\n\nYour selected file was not imported, please\nselect a valid mp3 file and try again.","Non-mp3 file selected", JOptionPane.WARNING_MESSAGE);
	    }
	    paintGUI(ipsmp.currentPlaylist);
	}
    }

    public void showNoFileSelectedPopup(){
	JOptionPane.showMessageDialog(frame,"No file selected. Please select an imported file\nbefore clicking play.\n\nIf you do not see any available files, try using the\nimport button to add MP3 files to your library.","No file selected", JOptionPane.WARNING_MESSAGE);
    }

    public class PlayListener implements ActionListener {
	IPSMP ipsmp;

	public PlayListener(IPSMP i){
	    ipsmp = i;
	}
	public void actionPerformed(ActionEvent a){
	    ipsmp.play(library.getSelectedValue());
	}
    }
    
    public class PauseListener implements ActionListener {
	IPSMP ipsmp;

	public PauseListener(IPSMP i){
	    ipsmp = i;
	}
	public void actionPerformed(ActionEvent a){
	    ipsmp.pause();
	}
    }

    public class NextListener implements ActionListener {
	IPSMP ipsmp;

	public NextListener(IPSMP i){
	    ipsmp = i;
	}
	public void actionPerformed(ActionEvent a){
	    ipsmp.nextTrack();
	}
    }
    
    public class PrevListener implements ActionListener {
	IPSMP ipsmp;
     
	public PrevListener(IPSMP i){
	    ipsmp = i;
	}
	public void actionPerformed(ActionEvent a){
	    ipsmp.prevTrack();
	}
    }

    public class PlayModeListener implements ActionListener {
	IPSMP ipsmp;
     
	public PlayModeListener(IPSMP i){
	    ipsmp = i;
	}
	public void actionPerformed(ActionEvent a){
	    ipsmp.playMode = a.getActionCommand();
	    System.out.println("Play mode set to " + a.getActionCommand());
	}
    }
}

 

